package Base;

import org.openqa.selenium.*;
import util.PageUtils;

/**
 * Created by ZH on 2018/1/26.
 */
public class BaseTest {

    private static BaseTest baseTest = new BaseTest();
    private WebDriver webDriver;

    public BaseTest(){
//        webDriver = PageUtils.getChromeDriver("http://b.easybuygold.com/merchant/login");
        webDriver = PageUtils.getChromeDriver("http://192.168.0.200:28080/merchant/login");
        WebElement account = webDriver.findElement(By.id("username"));
//        account.sendKeys("15211111111");
        account.sendKeys("13122222222");
        WebElement password = webDriver.findElement(By.name("password"));
//        password.sendKeys("1");
        password.sendKeys("123456");
        WebElement loginbtn = webDriver.findElement(By.id("login"));
        loginbtn.click();
    }

    public BaseTest(WebDriver w){
        webDriver = w;
//        webDriver = PageUtils.getChromeDriver("http://b.easybuygold.com/merchant/login");
        WebElement account = webDriver.findElement(By.id("username"));
        account.sendKeys("13122222222");
        WebElement password = webDriver.findElement(By.name("password"));
        password.sendKeys("nowqnucj");
        WebElement loginbtn = webDriver.findElement(By.id("login"));
        loginbtn.click();

    }

    public void maxWindow(){
        try {
            webDriver.manage().window().maximize();
        } catch (Error error) {
            System.err.println("maxWindow error");
            error.printStackTrace();
        }
    }

    /**
     * 切换到门店(内部调用)
     */
    private void toShop() throws InterruptedException {
        //关闭二维码
//        WebElement knowbtn = webDriver.findElement(By.xpath("//*[@id='wxModal']/div/button"));
//        knowbtn.click();
        /** ”我的门店“文字是否存在，不存在则需要切换到“我的门店” */
        try {
            webDriver.findElement(By.xpath("//li[contains(text(), '我的门店')]"));
        } catch (NoSuchElementException e) {
            Thread.sleep(2000);
            // 获取头像
            WebElement image = webDriver.findElement(By.xpath("//div[@id='head']/div/div/div/a/img"));
            image.click();
            Thread.sleep(2000);
            // 获取门店便签
            WebElement merchant = webDriver.findElement(By.xpath("//a[contains(text(), '安全退出')]/../following-sibling::li[2]/a"));
            merchant.click();
            Thread.sleep(2000);
            /** 关闭二维码 */
//            WebElement toast = webDriver.findElement(By.className("closebtn"));
//            toast.click();
        }
    }

    /**
     * 切换到商户(内部调用)
     */
    private void toMerchant() throws InterruptedException {
        //关闭二维码
//        WebElement knowbtn = webDriver.findElement(By.xpath("//*[@id='wxModal']/div/button"));
//        knowbtn.click();
        /** ”我的门店“文字是否存在，存在则需要切换到“商户” */
        try {
            if(webDriver.findElement(By.xpath("//*[@id=\"head\"]/div/div[1]/div[2]/ul/li[2]")) != null){
                Thread.sleep(2000);
                // 获取头像
                WebElement image = webDriver.findElement(By.xpath("//div[@id='head']/div/div/div/a/img"));
                image.click();
                Thread.sleep(2000);
                // 获取商户便签
                WebElement merchant = webDriver.findElement(By.xpath("//a[contains(text(), '安全退出')]/../following-sibling::li[2]/a"));
                merchant.click();
                Thread.sleep(2000);
                /** 关闭二维码 */
//                WebElement toast = webDriver.findElement(By.className("closebtn"));
//                toast.click();
            }
        }catch (NoSuchElementException e){
            System.err.println("switchToMerchant error");
//            e.printStackTrace();
        }

    }

    /**
     * 关闭微信二维码(内部调用)
     */
    private void closeWxQRCode() {
        try {
            //关闭二维码
            WebElement knowbtn = webDriver.findElement(By.xpath("//*[@id='wxModal']/div/button"));
            knowbtn.click();
        } catch (Error error) {
            System.err.println("关闭微信二维码错误");
            error.printStackTrace();
        }
    }

    /**
     * 切换到门店
     * @throws InterruptedException
     */
    public void switchToShop() throws InterruptedException {
        try {
            BaseTest.getInstance().closeWxQRCode();
            BaseTest.getInstance().toShop();
            BaseTest.getInstance().closeWxQRCode();
        }catch (ElementNotVisibleException e){
            System.out.println("二维码已被绑定");
            BaseTest.getInstance().toShop();
        }catch (NoSuchElementException e){
            System.out.println("二维码显示出错");
            BaseTest.getInstance().toShop();
        }
    }

    /**
     * 切换到商户
     * @throws InterruptedException
     */
    public void switchToMerchant() throws InterruptedException {
        try {
            BaseTest.getInstance().closeWxQRCode();
            BaseTest.getInstance().toMerchant();
            BaseTest.getInstance().closeWxQRCode();
        }catch (ElementNotVisibleException e){
            System.out.println("二维码已被绑定");
            BaseTest.getInstance().toMerchant();
        }catch (NoSuchElementException e){
            System.out.println("二维码显示出错");
            BaseTest.getInstance().toMerchant();
        }
    }

    /**
     * 菜单切换
     */
    public void selectMenu(String firstMenu, String secondMenu, String thirdMenu) throws InterruptedException {
        try {
            Thread.sleep(2000);
            webDriver.switchTo().defaultContent();

            if (firstMenu != null) {
                webDriver.findElement(By.linkText(firstMenu)).click();
               // System.out.println("点击一级菜单: " + firstMenu);
                Thread.sleep(1000);
            }

            if (secondMenu != null) {
                webDriver.findElement(By.linkText(secondMenu)).click();
               // System.out.println("点击二级菜单: " + secondMenu);
                Thread.sleep(1000);
            }

            if (thirdMenu != null) {
                webDriver.findElement(By.linkText(thirdMenu)).click();
               // System.out.println("点击三级菜单: " + thirdMenu);
                Thread.sleep(1000);
            }
        } catch (Error error) {
            System.err.println("select menu error");
            error.printStackTrace();
        }
    }

    public static BaseTest getInstance(){
        return baseTest;
    }

    public WebDriver getWebDriver(){
        return webDriver;
    }


}
